import sys
import os

sys.path.append("../../")
import nfs

class NfsCleanFailed(RuntimeError): pass

def cleanNfs(export):
    """
    Cleans an NFS export.
    Permission issues. Ask someone later.
    """
    mnt = os.path.abspath('mnt')
    try:
        os.mkdir(mnt)
    except:
        pass

    if nfs.mount(export, mnt) != 0:
        raise NfsCleanFailed("Could not mount NFS export.")

    try:
        if os.system("sudo rm -R -f %s" % os.path.join(mnt, "./*")) != 0:
            raise NfsCleanFailed("Could not clean export '%s'." % (export))
    finally:
        if nfs.umount(export, mnt) != 0:
            raise NfsCleanFailed("Could not unmount export. System left in unstable condition.")


